<?php
namespace addons\Contact\merchant\controllers;

use Yii;
use common\enums\AppEnum;
use common\models\merchant\Member;
use common\traits\Curd;
use common\models\base\SearchModel;

class DefaultController extends BaseController
{
    use Curd;

    public $modelClass = Member::class;

    public function actionIndex()
    {
        // 获取当前用户权限的下面的所有用户id，除超级管理员
        $ids = Yii::$app->services->rbacAuthAssignment->getChildIds(AppEnum::MERCHANT);

        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'relations' => ['department' => ['title']], // 关联 member 表的 nickname 字段
            'partialMatchAttributes' => ['username', 'mobile', 'realname','department.title'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC,
            ],
            'pageSize' => $this->pageSize,
        ]);

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->query
//            ->andFilterWhere(['in', 'id', $ids])
            ->andFilterWhere([Member::tableName().'.merchant_id' => $this->getMerchantId()])
            ->with('assignment');

        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }
}
